package com.rwaste.oss.controller;

import com.rwaste.commonutils.Msg;
import com.rwaste.oss.service.OssService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * oss文件管理
 * @author 魏小春
 */
@RestController
@RequestMapping("/oss")
public class OssCOntroller {

    @Autowired
    private OssService ossService;

    //用户id(用于上传时创建用户个人文件夹)
    private String userId;

    /**
     * 用户头像上传时文件夹的创建
     * @param userId 用户id
     * @return
     */
    @GetMapping("setUserId/{id}")
    public Msg setUserId(@PathVariable(name = "id", required = false) String userId){
        if(StringUtils.isEmpty(userId) || userId.equals("")){
            userId = "new_user";
        }
        this.userId = userId;
        return Msg.success();
    }

    /**
     * 头像上传
     * @param file 头像文件
     * @return 统一返回类型Msg
     */
    @PostMapping("/avatarUpload")
    public Msg avatarUpload(MultipartFile file){
        String url = ossService.uploadAvatar(file, userId);
        return Msg.success().add("imageUrl", url);
    }
}
